<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>汇捷通</title>
    <link rel="stylesheet" href="style/css/global.css">
    <link rel="stylesheet" href="style/css/news.css">
    <style>
        #pageId li{
            cursor: pointer;
        }
        .type ul li{
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <header>
            <div class="logo">
                <img src="images/logo.png" alt="汇捷通" />
                <span>汇捷通</span>
                <span>一站式外贸服务平台</span>
            </div>
            <div class="nav">
                <ul>
                    <li>
                        <a href="index.html">首页</a>
                    </li>
                    <li>
                        <a href="product.html">产品服务</a>
                    </li>
                    <li class="active">
                        <a href="news.html">新闻动态</a>
                    </li>
                    <li>
                        <a href="about.html">关于我们</a>
                    </li>
                    <li>
                        <a href="https://huijiet.com/index.html" target="_blank">进入操作平台</a>
                    </li>
                    <li>
                        <a href="https://huijiet.com/manager.html" target="_blank">后台管理</a>
                    </li>
                </ul>
            </div>
        </header>
        <section>
            <div class="bannar"></div>
            <div class="main">
                <div class="box">
                    <div class="menu">
                        <div class="title">
                            <h2>新闻动态</h2>
                            <p>NEWS CENTER</p>
                        </div>
                        <ul>
                            <li class="active">
                                <a href="news.html">贸易资讯</a>
                            </li>
                            <li>
                                <a href="company.html">公司动态</a>
                            </li>
                        </ul>
                    </div>
                    <div class="content">
                        <div class="title">贸易资讯</div>
                        <div class="type">
                            <ul>
                                <li class="active" id='news11'>
                                    <a onclick="changeType(11);">信保政策</a>
                                </li>
                                <li id='news12'>
                                    <a onclick="changeType(12);">海关政策</a>
                                </li>
                                <li id='news13'>
                                    <a onclick="changeType(13);">金融政策</a>
                                </li>
                            </ul>
                        </div>
                        <div class="list">
                            <ul id='ulId'>

                            </ul>
                        </div>
                        <div class="page">
                            <ul id='pageId'>

                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <footer>
            <div class="contact">
                <div class="tel">
                    <div class="free">
                        <img src="images/logo-2.png" />
                        <p>全国咨询热线</p>
                        <p>0710-3453029</p>
                    </div>
                    <ul>
                        <li>电话：0710-3453029</li>
                        <li>邮箱：huijietong666@163.com</li>
                        <li>网址：www.huijiet.com</li>
                    </ul>
                </div>
                <img src="images/qrcode.jpg" />
            </div>
            <div class="copyright">
                <ul>
                    <li>Copyright ©  2018-2020 襄阳汇捷通供应链管理有限公司 </li>
                    <li>地址：湖北自贸区(襄阳片区)米芾路陆港保税物流中心综合楼三楼</li>
                    <li><img src="images/110.png" /> 鄂ICP备17023599号-2</li>
                </ul>
            </div>
        </footer>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>

        var p = {
            page: 1,
            num: 5,
            newsType: 11,
            totalPage: 0
        }

        $(document).ready(function(){
            getData(p);
        });

        //获取数据
        function getData(param){

            var start = (param.page - 1) * param.num;
            $.ajax({
                type: 'POST',
                url: '/api/news/pageList',
                data: {
                    start: start,
                    num: param.num,
                    newsType: param.newsType
                },
                dataType: 'json',
                success: function(data){
                    console.log(data)
                    $("#ulId").empty();
                    var html = '';
                    $.each(data.data.rows, function(index, row){
                        html = html + '<li><a href="/details.html-'+row.newsId+'" target="_blank">'
                        + '<img src="'+row.imgViewUrl+'"/>'
                        + '<div class="txt">'
                        + '<h2>'+row.title+'</h2>'
                        + '<p>'+row.summary+'</p>'
                        + '<span>'+row.publishTime+'</span>'
                        + '</div></a></li>'
                    });
                    $("#ulId").html(html);

                    var total = data.data.total;
                    if(total%p.num==0){
                        p.totalPage = total/p.num;
                    }else{
                        p.totalPage = parseInt(total/p.num) + 1;
                    }
                    console.log(p.totalPage);
                    pagination();
                }
            });
        }


        //生成分页条
        function pagination(){
            $("#pageId").empty();
            var html = '';
            //没有数据，或者只有一页数据，不显示分页条
            if(p.totalPage==0||p.totalPage==1){
                html = '';
            }
            //小于等于5条
            else if(p.totalPage<=5){
                html = html + '<li onclick="goPage(1);">首页</li>';
                for(var i=1;i<=p.totalPage;i++){
                    if(i==p.page){
                        html = html + '<li onclick="goPage('+i+');" class="active">'+i+'</li>';
                    }else{
                        html = html + '<li onclick="goPage('+i+');">'+i+'</li>';
                    }
                }
                html = html + '<li onclick="goPage('+p.totalPage+');">尾页</li>';
            }
            //大于5条的情况
            else{
                html = html + '<li onclick="goPage(1);">首页</li>';
                if(p.page<=3){
                    for(var i=1;i<=5;i++){
                        if(i==p.page){
                            html = html + '<li onclick="goPage('+i+');" class="active">'+i+'</li>';
                        }else{
                            html = html + '<li onclick="goPage('+i+');">'+i+'</li>';
                        }
                    }
                }else if(p.page>=(p.totalPage-2)){
                    for(var i=(p.totalPage-4);i<=p.totalPage;i++){
                        if(i==p.page){
                            html = html + '<li onclick="goPage('+i+');" class="active">'+i+'</li>';
                        }else{
                            html = html + '<li onclick="goPage('+i+');">'+i+'</li>';
                        }
                    }
                }else{
                    for(var i=(p.page-2);i<=(p.page+2);i++){
                         if(i==p.page){
                             html = html + '<li onclick="goPage('+i+');" class="active">'+i+'</li>';
                         }else{
                             html = html + '<li onclick="goPage('+i+');">'+i+'</li>';
                         }
                    }
                }
                html = html + '<li onclick="goPage('+p.totalPage+');">尾页</li>';
            }
            $("#pageId").html(html);
        }

        //点击分页条切换页码
        function goPage(currentPage){
            p.page = currentPage;
            getData(p);
        }


        //切换新闻类别
        function changeType(type){
            if(type==11){
                $("#news11").addClass("active");
                $("#news12").removeClass("active");
                $("#news13").removeClass("active");
            }else if(type==12){
                $("#news11").removeClass("active");
                $("#news12").addClass("active");
                $("#news13").removeClass("active");
            }else if(type==13){
                $("#news11").removeClass("active");
                $("#news12").removeClass("active");
                $("#news13").addClass("active");
            }
            p.newsType = type;
            p.page = 1;
            getData(p);
        }



    </script>
</body>
</html>